package engine.newUi
{
	import com.greensock.TweenLite;
	import com.greensock.plugins.VisiblePlugin;
	import com.greensock.plugins.VolumePlugin;
	
	import engine.control.loading.MultLoader;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.logic.tips.TipsManage;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.text.NxLabel;
	import newx.utils.FilterUtil;

	public class GuessWordLogic
	{
		private static var _self : GuessWordLogic;
		private var _ui : GuessWordUI;
		private var _model : ModelLocator;
		private var _bindManage:BindManage;
		private var isBind : Boolean;
		
		private var secret : Array;
		private var LightMCArr : Array;
		private var rightAnswerNum : int;
		
		private var isFirstQuestion : Boolean;
		private var QuestionIsOver : Boolean;
		private var answer : String;
		private var currentIndex : int;
		private var question : int;
		/**所有问题*/
		private var queswerArr : Array;
		
		public function GuessWordLogic()
		{
		}
		
		public function Init():void
		{
			_model = ModelLocator.getInstance();
			_bindManage = BindManage.getInstance(); 
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUESS_ORDER),QuewtionSecret);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUESS_QUESTION),getQuestion);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_GUESS_PRICE),getPrize);
		}
		
		/**
		 *获得问题加密串 
		 * @param e
		 * 
		 */
		private function QuewtionSecret(e:CustomMsgEvent):void
		{
			var p:int;
			secret = [];
			for(var i:int;i<e.pList.length/2;i++)
			{
				var obj:Object = new Object();
				obj[e.pList[p++]] = e.pList[p++];
				secret.push(obj);
			}
		}
		
		/**
		 *获得所有问题 
		 * @param e
		 * 
		 */
		private function getQuestion(e:CustomMsgEvent):void
		{
			var p:int;
			queswerArr = [];
			for(var i:int;i<e.pList.length/5;i++)
			{
				var obj:Object = new Object();
				obj["questionId"] = e.pList[p++];
				obj["answer1"] = e.pList[p++];
				obj["answer2"] = e.pList[p++];
				obj["answer3"] = e.pList[p++];
				obj["isAnswer"] = int(e.pList[p++]);
				queswerArr.push(obj);
			}
			if(queswerArr[0]["isAnswer"] == 0)
			{
				currentIndex = 1;
				ui.startAnswer.x = 144;
				ui.startAnswer.scaleX = 1;
				ui.startAnswer.visible = true;
				setVisible(false);
				ui.endAnswer.visible = false;
			}
			else
			{
				setVisible(true);
				ui.startAnswer.visible = false;
			}
			if(queswerArr[4]["isAnswer"] != 0)
			{
				currentIndex = 5;
				setVisible(false);
				ui.startAnswer.visible = false;
				ui.endAnswer.visible = true;
			}
			setQuestion();
		}
		
		/**
		 *猜对灯谜获得奖励 
		 * @param e
		 * 
		 */
		private function getPrize(e:CustomMsgEvent):void
		{
			var gold : int = e.pList[0];
			var repute : int  = e.pList[1];
			var prizeLab : NxLabel = new NxLabel();
			prizeLab.htmlText = StaticIni.getSwfLangStr("Riddles_6");
			var index : int = rightAnswerNum%5;
			var light:MovieClip = LightMCArr[index] as MovieClip;
			light.addChild(prizeLab);
			TweenLite.to(prizeLab,2,{y:prizeLab.y-30,alpha:0,onComplete:function():void{light.removeChild(prizeLab)}});
		}
		
		
		public function BeforeShow():void
		{
			this._ui.visible = false;
//			//是否加载资源
			if(	_ui.BGMC == null)
			{
				MultLoader.getInstance().load([StageInfo.swfResPath+"GuessWord.swf"],true,CreateUI)
			}else
			{
				updataUI();
			}
		}
		
		public function AfterHide():void
		{
			removeEvent();
		}
		private function addEvent():void
		{
			this._ui.visible = true;
			
			ui.closeBtn.addEventListener(MouseEvent.CLICK,closeUI);
			ui.select1.addEventListener(MouseEvent.CLICK,onSelect);
			ui.select2.addEventListener(MouseEvent.CLICK,onSelect);
			ui.select3.addEventListener(MouseEvent.CLICK,onSelect);
			
			ui.startAnswer.addEventListener(MouseEvent.CLICK,startAnswer);
			ui.startAnswer.addEventListener(MouseEvent.ROLL_OVER,onstart);
			ui.startAnswer.addEventListener(MouseEvent.ROLL_OUT,overstart);
			ui.referBtn.addEventListener(MouseEvent.CLICK,referAnswer);
			ui.lingquBtn.addEventListener(MouseEvent.CLICK,onLingqu);
			
			ui.lingquBtn.addEventListener(MouseEvent.ROLL_OVER,onLingQuTips);
			ui.lingquBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			for(var i:int;i<LightMCArr.length;i++)
			{
				var mc:MovieClip = LightMCArr[i] as MovieClip;
				mc.addEventListener(MouseEvent.ROLL_OVER,onLightTips);
				mc.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			}
			
			//请求猜谜问题
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_GUESS_QUESTION);
		}	
		
		private function removeEvent():void
		{
			ui.closeBtn.removeEventListener(MouseEvent.CLICK,closeUI);
			ui.select1.removeEventListener(MouseEvent.CLICK,onSelect);
			ui.select2.removeEventListener(MouseEvent.CLICK,onSelect);
			ui.select3.removeEventListener(MouseEvent.CLICK,onSelect);
			
			ui.startAnswer.removeEventListener(MouseEvent.CLICK,startAnswer);
			ui.startAnswer.removeEventListener(MouseEvent.ROLL_OVER,onstart);
			ui.startAnswer.removeEventListener(MouseEvent.ROLL_OUT,overstart);
			ui.lingquBtn.removeEventListener(MouseEvent.CLICK,onLingqu);
			ui.referBtn.removeEventListener(MouseEvent.CLICK,referAnswer);
			
			ui.lingquBtn.removeEventListener(MouseEvent.ROLL_OVER,onLingQuTips);
			ui.lingquBtn.removeEventListener(MouseEvent.ROLL_OUT,hideTips);
			for(var i:int;i<LightMCArr.length;i++)
			{
				var mc:MovieClip = LightMCArr[i] as MovieClip;
				mc.removeEventListener(MouseEvent.ROLL_OVER,onLightTips);
				mc.removeEventListener(MouseEvent.ROLL_OUT,hideTips);
			}
		}
		
		/**
		 *第一次加载成功 这个只执行一次
		 * 
		 */
		private function CreateUI():void
		{
			_ui.windowBG.visible = false;
			_ui.BGMC = MultLoader.getInstance().getSourceByUrl(StageInfo.swfResPath+"GuessWord.swf");
			_ui.addChild(ui.BGMC);
			_ui.setChildIndex(_ui.BGMC,0);
			updataUI();
			
		}
		
		private function updataUI():void
		{
			LightMCArr = [_ui.BGMC["mc_1"],
				_ui.BGMC["mc_2"],
				_ui.BGMC["mc_3"],
				_ui.BGMC["mc_4"],
				_ui.BGMC["mc_5"]];
			_ui.questionBG = _ui.BGMC["mc_6"];
			_ui.questionLab = _ui.BGMC["question"];
			_ui.questionNumLab = _ui.BGMC["questionNum"];
			_ui.answerLab1 = _ui.BGMC["answer1"];
			_ui.answerLab2 = _ui.BGMC["answer2"];
			_ui.answerLab3 = _ui.BGMC["answer3"];
			_ui.select1 = _ui.BGMC["select1"];
			_ui.select2 = _ui.BGMC["select2"];
			_ui.select3 = _ui.BGMC["select3"];
			_ui.timesLab = _ui.BGMC["times"];
			_ui.timeLab = _ui.BGMC["timeLab"];
			_ui.countLab = _ui.BGMC["countLab"];
			_ui.startAnswer = ui.BGMC["start"];
			_ui.endAnswer = ui.BGMC["end"];
			_ui.endAnswer.visible = false;
			ui.startAnswer.gotoAndStop(1);
			setVisible(false);
			if(!isBind)
			{
				isBind = true;
				_bindManage.BindProp(_model.scene.Role,"GuessRightCount",this,"GuessRightCount");
			}
			addEvent();
		}
				
		/**
		 *已完成问题个数 
		 * @param val
		 * 
		 */
		public function set GuessRightCount(val :int):void
		{
			rightAnswerNum = val;
			if(val>=5)
			{
				ui.lingquBtn.filters = [];
			}
			else
			{
				ui.lingquBtn.filters = [FilterUtil.GrayColorFilter];
			}
			var timeStr :String = StaticIni.getSwfLangStr("Riddles_1");
			ui.timeLab.text = timeStr;
			var str:String = StaticIni.getSwfLangStrVar("Riddles_2",[int(val/5)])
			ui.countLab.htmlText = str;
			setLight(val);
		}
		
		/**
		 *选题答案 
		 * @param e
		 * 
		 */
		private function onSelect(e:MouseEvent):void
		{
			answer = e.target["answerId"];
			ui.referBtn.enabled = true;
			if(e.target == ui.select1)
			{
				setSelect(2,1,1);
			}
			else if(e.target == ui.select2)
			{
				setSelect(1,2,1);
			}
			else if(e.target == ui.select3)
			{
				setSelect(1,1,2);
			}
		}
		
		/**
		 *发送问题答案 
		 * @param e
		 * 
		 */
		private function referAnswer(e:MouseEvent):void
		{
			if(answer!=null)
			{
				sendAnswerMSG(int(answer));
			}
			else
			{
				
			}
		}
		
		private function onLingQuTips(e:MouseEvent):void
		{
			var str :String = StaticIni.getSwfLangStr("Riddles_4");
			TipsMainLogic.instance.showString(str);
		}
		
		private function onLightTips(e:MouseEvent):void
		{
			var str :String = StaticIni.getSwfLangStr("Riddles_3");
			TipsMainLogic.instance.showString(str);
		}
		
		private function hideTips(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		/**
		 *领取物品 
		 * @param e
		 * 
		 */
		private function onLingqu(e:MouseEvent):void
		{
			if(rightAnswerNum>=5)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUESS_GET_ITEM);
			}
		}
		
		/**
		 * 发送回答客户端回答的问题 和 答案
		 * @param question 问题 
		 * @param answer 答案
		 * 
		 */
		private function sendAnswerMSG(answerid:int):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUESS,question,answerid);
		}
		
		/**
		 *设置答案 
		 * @param type1
		 * @param type2
		 * @param type3
		 * 
		 */
		private function setSelect(type1:int,type2:int,type3:int):void
		{
			ui.select1.gotoAndStop(type1);
			ui.select2.gotoAndStop(type2);
			ui.select3.gotoAndStop(type3);
		}
		
		/**
		 *翻转 
		 * @param e
		 * 
		 */
		private function startAnswer(e:MouseEvent):void
		{
			TweenLite.to(ui.startAnswer,1,{scaleX:-1,x:_ui.startAnswer.x+_ui.startAnswer.width,visible:0,onComplete:function():void{setVisible(true)}});
		}
		
		private function onstart(e:MouseEvent):void
		{
			ui.startAnswer.gotoAndStop(2);
		}
		
		private function overstart(e:MouseEvent):void
		{
			ui.startAnswer.gotoAndStop(1);
		}
		
		/**
		 *设置问题的显示 
		 * @param boo
		 * 
		 */
		private function setVisible(boo:Boolean):void
		{
			ui.questionBG.visible = boo;
			ui.questionNumLab.visible = boo;
			ui.timesLab.visible =boo;
			ui.answerLab1.visible = boo;
			ui.answerLab2.visible = boo;
			ui.answerLab3.visible = boo;
			ui.select1.visible = boo;
			ui.select2.visible = boo;
			ui.select3.visible = boo;
			ui.questionLab.visible = boo;
			ui.referBtn.visible = boo;
		}
		
		/**
		 *根据加密的串返回该显示答案选项
		 * @param answer
		 * @return 
		 * 
		 */
		private function getAnswerId(answer : String):int
		{
			for(var i:int;i<secret.length;i++)
			{
				if(secret[i].hasOwnProperty(answer))
				{
					return int(secret[i][answer]);
				}
			}
			return 0;
		}
		
		/**
		 * 
		 * 设置问题的答案选项
		 */
		private function setQuestion():void
		{
			setSelect(1,1,1);
			answer = null;
			ui.referBtn.enabled = false;
			var currentQuestion : Object;
			var index : int;
			
			for(var i:int;i<queswerArr.length;i++)
			{
				var questionobj : Object = queswerArr[i];
				if(questionobj["isAnswer"] == 0)
				{
					currentQuestion = questionobj;
					index = i+1;
					break;
				}
			}
			if(currentQuestion != null)
			{
				ui.questionLab.htmlText = StaticIni.getSwfLangStr("RiddlesProblem_"+currentQuestion["questionId"]);
				ui.answerLab1.htmlText = StaticIni.getSwfLangStr("RiddlesAnswers_"+getAnswerId(currentQuestion["answer1"]));
				ui.answerLab2.htmlText = StaticIni.getSwfLangStr("RiddlesAnswers_"+getAnswerId(currentQuestion["answer2"]));
				ui.answerLab3.htmlText = StaticIni.getSwfLangStr("RiddlesAnswers_"+getAnswerId(currentQuestion["answer3"]));
				ui.select1["answerId"] = currentQuestion["answer1"];
				ui.select2["answerId"] = currentQuestion["answer2"];
				ui.select3["answerId"] = currentQuestion["answer3"];
				question = index-1;
				ui.questionNumLab.htmlText = StaticIni.getSwfLangStr("RiddlesNumber_"+index);
				ui.timesLab.htmlText = StaticIni.getSwfLangStr("Riddles_5");
			}
			else
			{
				
			}
		}
		
		/**
		 *设置亮灯 
		 * @param index
		 * 
		 */
		private function setLight(index:int):void
		{
			for(var j:int;j<5;j++)
			{
				if(index%5 != 0)
				{
					if(j<index%5 || index == 5)
					{
						(LightMCArr[j] as MovieClip).gotoAndStop(2);
					}
					else
					{
						(LightMCArr[j] as MovieClip).gotoAndStop(1);
					}
				}
				else
				{
					if(index>5 || index == 5)
					{
						(LightMCArr[j] as MovieClip).gotoAndStop(2);
					}
					else
					{
						(LightMCArr[j] as MovieClip).gotoAndStop(1);
					}
				}
			}
		}
		private function closeUI(e:MouseEvent):void
		{
			ui.hide();
		}
		
		public static function getInstance():GuessWordLogic
		{
			if(!_self)
			{
				_self = new GuessWordLogic();
			}
			return _self;
		}
		
		public function get ui():GuessWordUI
		{
			if(!_ui)
			{
				_ui = new GuessWordUI();
			}
			return _ui;
		}
		
	}
}